/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package forum.server.domain;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import forum.server.controller.Security;
import forum.server.persistence.DataManipulatorProxy;
import static org.junit.Assert.*;

/**
 *
 * @author Alon Furman
 */
public class LoggedInUserTest {

    public LoggedInUserTest() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
    	//DataManipulator.getInstance().resetDataBase();
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }




    /**
     * Test of addPost method, of class LoggedInUser.
     */
    @Test
    public void testAddPost() {
        String header = "Basketball";
        PostContent postContent = new PostContent("Hello");
        LoggedInUser ru = new LoggedInUser("testSubject 0", "123abc");
        Post testPost = ru.addPost(header, postContent, 999);
        assertEquals(testPost.getAuthor(),ru.getUsername());
    }

    
    @Test
    public void testModifyOwnPost(){
    	String header = "Basketball";
        PostContent postContent = new PostContent("Hello");
        LoggedInUser ru = new LoggedInUser("testSubject 0", "123abc");
        Post testPost = ru.addPost(header, postContent, 999);
        try{
            PostContent newPostContent = new PostContent("abc");
            ru.modifyPost(testPost, newPostContent);
            assertEquals(testPost.getContent(), newPostContent);
        }
        catch(Exception e){
        	fail("Exception was thrown!");
        }
    }
     /**
     * Test of modifyPost method, of class LoggedInUser
      * by trying to modify a post of other user.
     */
    @Test
    public void testModifyPostOfOtherUser() throws Exception {
        String header = "Basketball";
        PostContent postContent = new PostContent("Hello");
        LoggedInUser ru = new LoggedInUser("testSubject 0", "123abc");
        Post testPost = ru.addPost(header, postContent, 999);
        LoggedInUser tUser = new LoggedInUser("testSubject 1", "1234");
        try{
            PostContent newPostContent = new PostContent("abc");
            tUser.modifyPost(testPost, newPostContent);
            fail("Error no exception was thrown!");
        }
        catch(Exception e){
            assertEquals(testPost.getContent(), postContent);
        }
    }

    /**
     * Test of replyToPost method, of class LoggedInUser.
     */
    @Test
    public void testReplyToPost() {
        String header = "Basketball";

        PostContent postContent = new PostContent("Hello");
        LoggedInUser ru = new LoggedInUser("testSubject 0", "123abc");
        Post testPost = ru.addPost(header, postContent, 999);
        LoggedInUser tUser = new LoggedInUser("testSubject 1", "123");
        Post replyPost = tUser.replyToPost("Tennis", postContent, testPost.getPostId(), 1000);
        try{
        	replyPost = DataManipulatorProxy.getInstance().getPost(replyPost.getPostId());
        	assertEquals(replyPost.getParentId(), testPost.getPostId());
        }
        catch (Exception e){
        	System.out.println("Error");
        }
    }

    
    @Test
    public void deleteOwnPost() {
        String header = "Basketball";
        PostContent postContent = new PostContent("Hello");
        LoggedInUser ru = new LoggedInUser("testSubject 0", "123abc");
        Post testPost = ru.addPost(header, postContent, 999);
        LoggedInUser tUser = new LoggedInUser("testSubject 1", "123");
        Post replyPost = tUser.replyToPost("Tennis", postContent, testPost.getPostId(), 1000);
        try{
        	ru.deletePost(testPost);
        }
        catch (Exception e){
        	fail("Exception was thrown!");
        }
    }
    
    @Test
    public void deleteOtherPost() {
        String header = "Basketball";
        PostContent postContent = new PostContent("Hello");
        LoggedInUser ru = new LoggedInUser("testSubject 0", "123abc");
        Post testPost = ru.addPost(header, postContent, 999);
        LoggedInUser tUser = new LoggedInUser("testSubject 1", "123");
        Post newPost = tUser.addPost("Tennis", postContent,1000);
        try{
        	ru.deletePost(newPost);
        	fail("No exception was thrown!");
        }
        catch (Exception e){
        	
        }
    }
    
    /**
     * Test of validatePassword method, of class LoggedInUser.
     */
    @Test
    public void testValidatePassword() {
        String password = "123ABC";
        try{
        	LoggedInUser ru = new LoggedInUser("testSubject 0", Security.encryptPassword("123abc"));
        	boolean expResult = false;
        	boolean result = ru.validatePassword(password);
        	assertEquals(expResult, result);
        	password = "123abc";
        	expResult = true;
        	result = ru.validatePassword(password);
        	assertEquals(expResult, result);
        }
        catch (Exception e){
        	fail("No exception expected.");
        }
    }

}